package Main;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;


public class Reader {
	
	

	public ArrayList<Attribute> read(){
		Integer ad = 0;
		ArrayList<Attribute> attributes = new ArrayList<Attribute>();
		try{
			FileInputStream fis = new FileInputStream("int3.txt");
			DataInputStream ds = new DataInputStream(fis);
			BufferedReader br = new BufferedReader(new InputStreamReader(ds));
			
			String line = "";
			boolean data = false;
			
			
		
			
			
			while((line = br.readLine()) != null){
				System.out.println(line);
				boolean shallBreak = false;
				int i = 0;
				char c = 'a';
				
				// check for empty line.
				if(line.length() != 0){
					 c = line.charAt(0);
					
				}else{
					shallBreak = true;
				}

				//To skip white spaces at the beginning.
				while(!shallBreak ){
					//skipping chars at at bad way for now...
					i++;
					if(i < line.length() && c == ' '){
						c = line.charAt(i);
						
					}else{
						i--;
						shallBreak = true;
					}

				}
				//Checks for comment.
				if(line.length() != 0 && line.charAt(i) != '%'){
					if(line.contains("@RELATION")){
						line = (String) line.subSequence(i+10, line.length());
					}else if(line.contains("@ATTRIBUTE") || line.contains("@attribute")){
						line = (String) line.subSequence(i+11, line.length());
                         String[] lines = line.split(" ");
                         
                         //checks which attribute type and do the correct things.
						if(lines[lines.length-1].equals("NUMERIC") || lines[lines.length -1].equals("numeric")){
							Attribute a = new Attribute(lines[0]);
							attributes.add(a);
						}else if(line.contains("{")){
							Attribute a = new Attribute(lines[0]);
							String name = lines[0];
							lines = line.split("\\{");
							lines = lines[1].split(",");
							
							for(int s= 0; s < lines.length-1; s++){
								a.addDataType(lines[s]);
							}
							
							
							a.addDataType(lines[lines.length-1].substring(0, lines[lines.length-1].length()-1));
							attributes.add(a);
						}
						//flag that data will now begin.
					}else if(line.contains("@DATA") || line.contains("@data")){
						data = true;
						//for every attribute, adds the data.
					}else if(data){

                       
						line = (String) line.subSequence(i, line.length());
						String[] split = line.split(",");				
						for(int j = 0; j < split.length ; j++){
							
								attributes.get(j).addData((split[j]));
							
							
						}

						
					}
				}
			}
			
					
			
		}catch(IOException e){
		
		}
		
		
		return attributes;
		
	}		
	
}
